<template>
  <div style="height: 100%" class="index">
    <el-dialog title="编辑页面"
               :visible.sync="dialogFormVisible"
               width="25%"
               :before-close="gb">
       <el-form :model="formM" :rules="rulesM" ref="formM"  label-width="100px">
          <el-form-item label="新密码" prop="pas" >
              <el-input v-model="formM.pas" clearable></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="npas" >
              <el-input v-model="formM.npas" clearable></el-input>
          </el-form-item>
       </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveM()" :disabled="saveBtnM">确 定</el-button>
        <el-button @click="qx()">取 消</el-button>
      </div>
    </el-dialog>


    <el-container style="height: 100%; width: auto; border: 1px solid #eee">
      <!-- 头部 -->
      <el-header style="text-align: right; font-size: 12px">
        <span style="float: left; font-size: 1.875rem;font-family: PingFang SC;">Vue--elm成绩管理</span>
        <el-dropdown style="margin-right: 1.875rem;" @command="click">
          <span class="el-dropdown-link"><i class="el-icon-s-tools"></i>&nbsp;&nbsp;{{this.name}}</span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item class="el-icon-switch-button" command="logout">&nbsp;&nbsp;退出登录</el-dropdown-item><br />
            <el-dropdown-item class="el-icon-key" command="edpas" >&nbsp;&nbsp;修改密码</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </el-header>
      <!-- 头部 -->
      <el-container>
        <!-- 导航 -->
        <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
          <el-menu :default-active="$router.path" router='router'>
            <el-submenu index="1">
              <template slot="title"><i class="el-icon-message"></i>用户操作</template>
              <el-menu-item-group>
                <el-menu-item index="/mune/kc">课程管理</el-menu-item>
                <el-menu-item index="/mune/bj">班级管理</el-menu-item>
                <el-menu-item index="/mune/student">学生管理</el-menu-item>
                <el-menu-item index="/mune/dept">部门管理</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title"><i class="el-icon-menu"></i>系统设置</template>
              <el-menu-item-group>
                <el-menu-item index="/mune/stuJo">班级成绩查询</el-menu-item>
                <el-menu-item index="/mune/grPsn">个人成绩录入</el-menu-item>
                <el-menu-item index="/mune/stuCx" >班级成绩录入</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 导航 -->

        <!-- 内容 -->
        <el-main>
          <router-view></router-view>
        </el-main>

        <!-- 内容 -->
      </el-container>
    </el-container>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        name:'游客',
        dialogFormVisible: false,
        formM:{
          pas:'',
          npas:''
        },
        saveBtnM:false,
        rulesM:{
          pas:[
            { required: true, message: '请输入密码', trigger: 'blur' },
          ],
          npas:[
            { required: true, message: '请确认密码', trigger: 'blur' },
          ],
        },
      }
    },
    methods: {
      gb(done){
        emptyObj(this.form)
        done();
      },
      qx(){
        emptyObj(this.form)
        this.dialogFormVisible = false
      },
      click(command){
        if (command=="logout") {
          this.logout()
        } else if(command=="edpas"){
          this.dialogFormVisible = true
        }
      },
      logout(){
        this.$ajax.post('logout',{}).then(res=>{
          var result = res.data
          if (result.success) {
            window.sessionStorage.removeItem("token")
            window.sessionStorage.removeItem("name")
            this.$router.replace("/")
          }
        })
      },
      saveM(){
        this.$refs.formM.validate(valid=>{
          this.saveBtn=true
          if (valid) {
            this.$ajax.post('uppwd',this.formM).then(res=>{
            console.log(1234)
              var result = res.data
              if (result.success) {
                this.$notify({
                  type:'success',
                  title: '提示',
                  message: '密码修改成功',
                  position: 'bottom-right'
                });
                this.saveBtn=false
                this.dialogFormVisible = false
                emptyObj(this.formM)
                this.logout()
              }else{
                this.$notify({
                  type:'error',
                  title: '提示',
                  message: '密码修改失败',
                  position: 'bottom-right'
                });
                this.dialogFormVisible = false
                emptyObj(this.formM)
              }

            })
          }else{
          this.saveBtn=false
          }
        })
      }

    },
    mounted() {
      this.name=window.sessionStorage.getItem("name")
    }


  };
  function emptyObj(obj){
    for( var key in obj){
      var o = obj[key];
      if(o instanceof Object){
        emptyObj(o);
      }else{
        obj[key]='';
      }
    }
  }
</script>

<style>
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
  }

  .el-aside {
    color: #333;
  }

  .index {
    height: calc(100vh);
  }

  html,
  body,
  #app,
  .el-container {
    padding: 0px;
    margin: 0px;
    height: 100%;
  }
</style>
